fn main() {
    // let v : Vec(i32) = Vec::new();
    
    let mut v: Vec<i32> = vec![1, 2, 3];

    // updating vector
    v.push(4);
    v.push(5);


    let mut s: Vec<String> = vec![];
    s.push(String::from("a"));
    s.push(String::from("b"));

    // reading elements of vectors
    let third: &i32 = &v[2];
    println!("the third element is {third}");

    let third: Option<&i32> = v.get(2);
    match third {
        Some(third) => println!("the third element is {third}"),
        None => println!("There is no third element."),
    }

    let mut v: Vec<i32> = vec![1, 2, 3, 4, 5];

    let mut first: &i32 = &v[0];

    v.push(6);
    // println!("The first element is: {first}"); // first was droped while v.push(6);

    // Iterating Over the Values in a Vector
    let v = vec![100,32,56];
    for i in &v{
        println!("{i}");
    }

    let mut v: Vec<i32> = vec![100, 32, 57];
    for i in &mut v {
        *i += 50;
    }

    // Using an Enum to Store Multiple Types
    enum SpreadsheetCell {
        Int(i32),
        Float(f64),
        Text(String),
    }

    let row = vec![
        SpreadsheetCell::Int(3),
        SpreadsheetCell::Text(String::from("blue")),
        SpreadsheetCell::Float(10.12),
    ];

    // Dropping a Vector Drops Its Elements
    {
        let v = vec![1, 2, 3, 4];

        // do stuff with v
    } // <- v goes out of scope and is freed here

    //  exercises
    // Given a list of integers, use a vector and return the median (when sorted, 
    // the value in the middle position) and mode (the value that occurs most often; a hash map will be helpful here) of the list.

    let mut datas: Vec<i32> = vec![10,4,2,7,9,2,5,4,4];
    let index: usize = datas.len() / 2;
    datas.sort();
    let mid: &i32 = &datas[index];
    println!("the middle value is {mid}");

    use std::collections::HashMap;
    let mut map: HashMap<i32, i32> = HashMap::new();
    for word in datas {
        let count: &mut i32 = map.entry(word).or_insert(0);
        *count += 1;
    }
    println!("{map:?}");
    
    let mut oftenValue : i32 = 0;
    let mut oftenKey : i32 = 0;
    for (key,value) in map {
        if value >= oftenValue {
            oftenValue = value;
            oftenKey = key;
        }
    }

    println!("the often value:{oftenKey} times:{oftenValue}")

}   
